package com.foreveross.proxyip.web.action.operate;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.foreveross.proxyip.application.operate.IpWebsiteEntityApplication;
import com.foreveross.proxyip.application.operate.vo.IpWebsiteEntityVo;
import com.foreveross.proxyip.exception.ProxyIpServiceException;
import com.foreveross.proxyip.infra.PageData;
import com.foreveross.proxyip.web.action.BaseAction;

@SuppressWarnings("serial")
@Controller
@org.springframework.context.annotation.Scope("prototype")
@Namespace("/operate/ipWebsiteManager")
@ParentPackage("json-default")
public class IpWebsiteManagerAction extends BaseAction {
	@Autowired
	IpWebsiteEntityApplication ipWebsiteEntityApplication;
	
	PageData pageData;	
	
	private int pageIndex;

	private int pageLen;
	
	private IpWebsiteEntityVo vo;
	
	private List<String> res;
	
	@Action(value="toJsp",results={@Result(name="dispatcher", type="dispatcher",location="/operate/ipWebsiteManager.jsp")})
	public String toJsp()throws ProxyIpServiceException{	
		return "dispatcher";
	}
	
	/**
	 * 查找所有IpWebsiteEntity实体
	 * @return
	 */
	@Action(value="search", results={@Result(name="JSON", type="json", params={"root","pageData"})})
	public String search()throws Exception{
		pageData=ipWebsiteEntityApplication.findAllIpWebsite(vo, pageIndex, pageLen);
		return "JSON";
	}
	
	
	/**
	 * 新增网站
	 * @return
	 * @throws Exception
	 */
	@Action(value="addIpWebsite", results={@Result(name="JSON", type="json", params={"root","pageData"})})
	public String addIpWebsite()throws Exception{
		//获取当前用户名称
		String userName=((org.springframework.security.core.context.SecurityContextImpl)super.getSession().getAttribute("SPRING_SECURITY_CONTEXT")).getAuthentication().getName();
		vo.setUserName(userName);
		
		ipWebsiteEntityApplication.addIpWebsite(vo);

		return "JSON";
	}
	
	
	/**
	 * 修改网站
	 * @return
	 * @throws Exception
	 */
	@Action(value="updateIpWebsite", results={@Result(name="JSON", type="json", params={"root","pageData"})})
	public String updateIpWebsite()throws Exception{

		ipWebsiteEntityApplication.updateIpWebsite(vo);
		
		return "JSON";
	}
	
	@Action(value="deleteWebsite", results={@Result(name="JSON", type="json", params={"root","pageData"})})
	public String deleteWebsite()throws Exception{

		ipWebsiteEntityApplication.deleteIpWebsite(vo);
		
		return "JSON";
	}
	/**
	 * 验证配置的网站
	 * @return
	 * @throws Exception
	 */
	@Action(value="checkWebsite", results={@Result(name="JSON", type="json", params={"root","res"})})
	public String checkWebsite()throws Exception{

		res=ipWebsiteEntityApplication.checkWebsite(vo);
		
		return "JSON";
	}
	
	
	public IpWebsiteEntityApplication getIpWebsiteEntityApplication() {
		return ipWebsiteEntityApplication;
	}

	public void setIpWebsiteEntityApplication(
			IpWebsiteEntityApplication ipWebsiteEntityApplication) {
		this.ipWebsiteEntityApplication = ipWebsiteEntityApplication;
	}

	public PageData getPageData() {
		return pageData;
	}

	public void setPageData(PageData pageData) {
		this.pageData = pageData;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageLen() {
		return pageLen;
	}

	public void setPageLen(int pageLen) {
		this.pageLen = pageLen;
	}

	public IpWebsiteEntityVo getVo() {
		return vo;
	}

	public void setVo(IpWebsiteEntityVo vo) {
		this.vo = vo;
	}

	public List<String> getRes() {
		return res;
	}

	public void setRes(List<String> res) {
		this.res = res;
	}
	
	


}
